package com.jshaz.xrichtextplus;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

/**
 * Created by jshaz on 2018/2/11.
 * 图像处理类
 */

public class ImageOperator {

    /**
     * 图片转成string
     *
     * @param bitmap 输入的Bitmap类型图片
     * @return 转换后的String
     */
    public static String convertBitmapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();// 转为byte数组
        return Base64.encodeToString(b, Base64.DEFAULT);

    }

    public static byte[] convertBitmapToByte(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();// 转为byte数组
        return b;

    }

    /**
     * String转成bitmap
     *
     * @param st 输入的String
     * @return bitmap 输出的Bitmap，若格式错误则输出null
     */
    public static Bitmap convertStringToBitmap(String st) {
        Bitmap bitmap = null;

        try
        {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(st, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
//             bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            return bitmap;
        }
        catch (Exception e)
        {
            return null;
        }

    }

    /**
     * 尺寸压缩图片
     * @param bitmap
     * @return
     */
    public static Bitmap compressBitmap(Bitmap bitmap) {
        if (bitmap == null) return null;
        Matrix matrix = new Matrix();
        matrix.setScale(0.5f, 0.5f);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true);
    }

    /**
     * 尺寸压缩图片
     * @param bitmap
     * @return
     */
    public static Bitmap compressBitmap(Bitmap bitmap, float scale) {
        if (bitmap == null) return null;
        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true);
    }

    public static Bitmap compressBitmap(Bitmap bitmap, float x, float y) {
        if (bitmap == null) return null;
        Matrix matrix = new Matrix();
        matrix.postScale(x, y);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true);
    }

    /**
     * 压缩字符串图片
     * @param bitmapString
     * @return
     */
    public static Bitmap compressBitmap(String bitmapString) {
        Bitmap t = convertStringToBitmap(bitmapString);
        return compressBitmap(t);
    }


    public static int dip2px(Context context, float dipValue) {
        float m = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * m + 0.5f);
    }


}
